package com.dong.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dong.usercenter.common.BaseResponse;
import com.dong.usercenter.common.ErrorCode;
import com.dong.usercenter.common.ResultUtils;
import com.dong.usercenter.exception.BusinessException;
import com.dong.usercenter.model.domain.User;
import com.dong.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

import static com.dong.usercenter.contant.UserConstant.ADMIN_ROLE;
import static com.dong.usercenter.contant.UserConstant.USER_LOGIN_STATE;

/**
 * @Author:Dong
 * @Date: 2024/5/22  17:15
 * @Version 1.0
 */
@RestController
public class selectController {

    @Resource
    private UserService userService;

    @GetMapping("/rule")
    public BaseResponse<List<User>> searchUsersBygetRules(@RequestParam(defaultValue = "1")int current,
                                                          @RequestParam(defaultValue = "1")int pageSieze) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        if (StringUtils.isNotBlank(username)) {
//            queryWrapper.like("username", username);
//        }

        List<User> userList = userService.list(queryWrapper);
        List<User> list = userList.stream().map(user -> userService.getSafetyUser(user)).collect(Collectors.toList());
        return ResultUtils.success(list);
    }

    /**
     * 是否为管理员
     *
     * @param request
     * @return
     */
    private boolean isAdmin(HttpServletRequest request) {
        // 仅管理员可查询
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        return user != null && user.getUserRole() == ADMIN_ROLE;
    }
}
